<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basepackage}.${table.className}Do">
	<#assign left_brace="{" />
	<#assign right_brace="}" />
    <!--字段映射-->
	<resultMap id="BaseResultMap" type="${basepackage}.${table.className}Do">
		<#list table.columns as column>
		<#if column.parmaryKey>
		<id column="${column.columnName}" property="${column.propertyName}" />
		<#else>
		<result column="${column.columnName}" property="${column.propertyName}" />
		</#if>
		</#list>
	</resultMap>

    <!--表名-->
	<sql id="Table_Name">
		${table.tableName}
	</sql>

    <!--基本字段-->
	<sql id="Base_Column">
		<#list table.columns as column>
		<#if column_has_next>
		${column.columnName},
		<#else>
		${column.columnName}
		</#if>
		</#list>
	</sql>

	<sql id="Base_Where">
		<trim suffixOverrides="AND">
            <#list table.columns as column>
            <#if column.javaType=="java.lang.String">
            <if test="${column.propertyName} != null and ${column.propertyName}.length()>0">
            <#else>
            <if test="${column.propertyName} != null">
            </#if>
                ${column.columnName}= #${left_brace}${column.propertyName}${right_brace}
                <#if column_has_next>
                AND
                </#if>
            </if>
            </#list>
		</trim>
	</sql>
	
    <sql id="Base_Select">
		SELECT
		<include refid="Base_Column" />
		FROM
		<include refid="Table_Name" />
		<where>
			<include refid="Base_Where" />
		</where>
	</sql>

    <!--插入-->
    <insert id="insert" >
		INSERT INTO
		<include refid="Table_Name" />
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<@generateInsertColumn/>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<@generateInsertValue/>
		</trim>
	</insert>

    <!--更新-->
	<update id="update" >
		UPDATE
		<include refid="Table_Name" />
		<set>
			<@generateUpdate/>
		</set>
		WHERE 
		  <#list table.pkColumns as column>
			${column.columnName}=#${left_brace}${column.propertyName}${right_brace}
			<#if column_has_next>
	        AND 
	        </#if>
		  </#list>
	</update>

    <!--更新-->
	<update id="updateByEntity" >
		UPDATE
		<include refid="Table_Name" />
		<set>
			<#list table.columns as column>
			<#if !column.parmaryKey>
			<if test="s.${column.propertyName} != null">
			      <#if column_has_next>
                    ${column.columnName}=#${left_brace}s.${column.propertyName}${right_brace},
                    <#else>
                    ${column.columnName}=#${left_brace}s.${column.propertyName}${right_brace}
                   </#if>
			</if>
			</#if>
			</#list>
		</set>
		<where> 
			<trim prefix="(" suffix=")" suffixOverrides="AND">
			<#list table.columns as column>
			<if test="w.${column.propertyName} != null">
                    ${column.columnName}=#${left_brace}w.${column.propertyName}${right_brace}
               <#if column_has_next>
                   AND 
               </#if>
 			</if>
			</#list>
			</trim>
		</where>
	</update>

    <!--删除-->
	<delete id="delete" >
		DELETE FROM
		<include refid="Table_Name" />
		<where>
			<include refid="Base_Where" />
		</where>
	</delete>

    <!--总数-->
	<select id="count" parameterType="java.util.Map" resultType="int">
		SELECT COUNT(1) FROM
		<include refid="Table_Name" />
		<where>
			<include refid="Base_Where" />
		</where>
	</select>

    <!--单条查询-->
	<select id="selectOne"  resultMap="BaseResultMap">
	  <include refid="Base_Select" />
	</select>

    <!--列表查询-->
	<select id="selectList"  resultMap="BaseResultMap">
	    <include refid="Base_Select" />
	</select>

    <!--锁表查询-->
     <select id="selectForUpdate" resultMap="BaseResultMap" useCache="false">
		 <include refid="Base_Select" />
		FOR UPDATE
	</select>
	
	<#macro generateUpdate>
	<#list table.columns as column>
			<#if column.javaType=="java.lang.String">
			<if test="${column.propertyName} != null and ${column.propertyName}.length()>0">
			<#else>
			<if test="${column.propertyName} != null">
			</#if>
			${column.columnName}=#${left_brace}${column.propertyName}${right_brace},
			</if>
	</#list>
	</#macro>

	<#macro generateInsertColumn>
	<#list table.columns as column>
			<if test="${column.propertyName} != null">
				${column.columnName},
			</if>
	</#list>
	</#macro>
	<#macro generateInsertValue>
	<#list table.columns as column>
			<if test="${column.propertyName} != null">
			#${left_brace}${column.propertyName}${right_brace},
			</if>
	</#list>
	</#macro>

</mapper>